package org.caro.gui;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JTextArea;
import java.awt.Insets;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.Box;
import org.caro.actions.ContestantAction;
import org.caro.exceptions.ConnectionException;
import org.caro.utils.Constants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChatZonePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Create the panel.
	 */
	private ChatZoneArea chatZone;
	private JButton sendButton;
	private JTextArea chatArea;
	private ContestantAction contestant;

	public ChatZonePanel() {
		setVisible(true);
	}

	public ChatZonePanel(int mode,ContestantAction contestant) throws Exception {
		this.contestant = contestant;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 317, 0, 66, 160, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		Component verticalStrut_1 = Box.createVerticalStrut(60);
		GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
		gbc_verticalStrut_1.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut_1.gridx = 0;
		gbc_verticalStrut_1.gridy = 0;
		add(verticalStrut_1, gbc_verticalStrut_1);

		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		add(scrollPane, gbc_scrollPane);

		chatZone = new ChatZoneArea();
		scrollPane.setViewportView(chatZone);

		Component verticalStrut = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
		gbc_verticalStrut.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut.gridx = 0;
		gbc_verticalStrut.gridy = 2;
		add(verticalStrut, gbc_verticalStrut);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 3;
		add(panel, gbc_panel);
		panel.setLayout(new BorderLayout(0, 0));

		sendButton = new JButton("  G\u1EEDi  ");
		sendButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String message = chatArea.getText();
				try {
					ChatZonePanel.this.contestant.getChatSend().doAction(message, chatZone);
					chatArea.setText("");
				} catch (ConnectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		panel.add(sendButton, BorderLayout.EAST);

		JScrollPane scrollPane_1 = new JScrollPane();
		panel.add(scrollPane_1, BorderLayout.CENTER);

		chatArea = new JTextArea();
		scrollPane_1.setViewportView(chatArea);
		setPreferredSize(new Dimension(250, 750));
		setChatZoneByMode(mode);
	}

	private void setChatZoneByMode(int mode) throws Exception {
		if (mode == Constants.AI_MODE) {
			sendButton.setEnabled(false);
		} else if (mode == Constants.LAN_MODE) {
			chatZone = new ChatZoneArea(contestant.getHome(), contestant.getAway());
			contestant.getChatRecv().doAction(chatZone);
		}
	}

	public ChatZoneArea getDisplayArea() {
		return chatZone;
	}

	public JButton getSendButton() {
		return sendButton;
	}

	public JTextArea getChatArea() {
		return chatArea;
	}

}
